<?php

class MenuController extends AController {

    protected $restrictedId = array(1); //những ID không cho update hoặc delete

    public function actionIndex() {
        $model = DMenu::model();
        $this->render('index', array('model' => $model));
    }

    public function actionCreate($parentId = '') {
        $this->layout = '//layouts/admin/main_none';
        $model = new DMenu;

        if (!empty($parentId))
            $model->parent_id = $parentId;

        if (isset($_POST['DMenu'])) {
            $model->attributes = $_POST['DMenu'];
            if ($model->save())
                Yii::app()->clientScript->registerScript('close-dialog', 'parent.$("#dialogForm").dialog("close");'
                );
        }

        $this->render('form', array(
          'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        if (array_key_exists($id, $this->restrictedId))
            throw new CHttpException(403, 'This is a root level. You can not modify.');

        $this->layout = '//layouts/admin/main_none';
        $model = $this->loadModel($id);

        if (isset($_POST['DMenu'])) {
            $model->attributes = $_POST['DMenu'];
            if ($model->save())
                Yii::app()->clientScript->registerScript('close-dialog', 'parent.$("#dialogForm").dialog("close");'
                );
        }

        $this->render('form', array(
          'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->request->isAjaxRequest) {
            $model = $this->loadModel($id);
            if ($model->canDelete($id)) {
                $model->delete();
                $result['success'] = true;
                $result['message'] = Yii::t('DadminModule.msg', 'deleted');
            }
            else {
                $result['success'] = false;
                $result['message'] = Yii::t('DadminModule.msg', 'menu_deny_delete');
            }

            echo json_encode($result);
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    /**
     * Show childs of Parent menu - AJAX
     * @param integer $parentId
     */
    public function actionMenuShowChild($parentId) {
        $menu = DMenu::model()->getMenuChild($parentId);
        if (Yii::app()->request->isAjaxRequest) {
            $this->renderPartial('_menu_child', array(
              'menu'     => $menu,
              'parentId' => $parentId,
            ));
        }
        else
            throw new CHttpException(403); //forbidden
    }

    public function actionMenuShowFull() {
        $model = DMenu::model();

        $this->renderPartial('_menu_full', array(
          'model' => $model,
        ));
    }

    /**
     * Update sort order - AJAX
     */
    public function actionMenuUpdateOrder() {
        if ((Yii::app()->request->isAjaxRequest) && (isset($_POST['orderid']))) {
            $model     = DMenu::model();
            $orderList = $_POST['orderid'];

            foreach ($orderList as $order => $typeId) {
                $record = $model->findByPk($typeId);
                $record->sort_order = $order;
                $record->save();
            }
        }
        else
            throw new CHttpException(403); //forbidden
    }

    public function loadModel($id) {
        $model = DMenu::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'menu-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
